<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>账户信息 — 银行客户系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            overflow-x: hidden;
        }

        .app-container {
            display: flex;
            min-height: 100vh;
            width: 100%;
        }

        .sidebar {
            width: 250px;
            background: linear-gradient(to bottom, #1a3a6c, #2c5a8d);
            color: #fff;
            padding: 30px 0;
            flex-shrink: 0;
        }

        .logo-container {
            padding: 0 25px 25px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .logo-icon {
            width: 50px;
            height: 50px;
            background-color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #1a3a6c;
            font-weight: bold;
        }

        .logo-text {
            font-size: 20px;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
            padding: 20px 25px;
            gap: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: #ffcc00;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 20px;
        }

        .user-details {
            flex: 1;
        }

        .user-name {
            font-weight: 600;
            margin-bottom: 5px;
            font-size: 16px;
        }

        .user-account {
            font-size: 13px;
            opacity: 0.8;
        }

        .sidebar-menu {
            padding: 0 15px;
        }

        .menu-item {
            padding: 14px 20px;
            display: flex;
            align-items: center;
            gap: 15px;
            cursor: pointer;
            transition: all 0.2s;
            border-radius: 8px;
            margin-bottom: 8px;
        }

        .menu-item:hover {
            background: rgba(255, 255, 255, 0.1);
        }

        .menu-item.active {
            background: rgba(255, 255, 255, 0.15);
            border-left: 4px solid #ffcc00;
        }

        .menu-item i {
            font-size: 18px;
            width: 24px;
            text-align: center;
        }

        .menu-text {
            font-size: 15px;
        }

        /* 右侧内容区样式 */
        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            overflow: auto;
        }

        .topbar {
            background-color: white;
            padding: 0 30px;
            height: 80px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-bottom: 1px solid #eee;
            flex-shrink: 0;
        }

        .page-title {
            font-size: 24px;
            font-weight: 600;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .page-title i {
            background-color: #1a3a6c;
            color: white;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
        }

        .current-time {
            font-size: 14px;
            color: #666;
            font-weight: 500;
        }

        .content-area {
            flex: 1;
            padding: 30px;
            overflow-y: auto;
            background-color: #f8fafc;
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        /* 余额查询页面样式 */
        .balance-container {
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        .balance-info-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            padding: 30px;
        }

        .section-title {
            font-size: 20px;
            color: #1a3a6c;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #1a3a6c;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .section-title i {
            color: #1a3a6c;
        }

        .account-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin-bottom: 30px;
        }

        .detail-item {
            display: flex;
            flex-direction: column;
            gap: 5px;
        }

        .detail-label {
            font-size: 14px;
            color: #666;
            font-weight: 500;
        }

        .detail-value {
            font-size: 18px;
            font-weight: 600;
            color: #1a3a6c;
        }

        .balance-display {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 40px 0;
            border-top: 1px dashed #ddd;
            border-bottom: 1px dashed #ddd;
            margin: 30px 0;
        }

        .balance-label {
            font-size: 18px;
            color: #666;
            margin-bottom: 15px;
        }

        .balance-amount {
            font-size: 48px;
            font-weight: 700;
            color: #1a3a6c;
            letter-spacing: 1px;
        }

        .balance-currency {
            font-size: 28px;
            font-weight: 600;
            color: #1a3a6c;
            margin-right: 10px;
        }

        .btn-refresh {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background-color: #1a3a6c;
            color: white;
            padding: 12px 25px;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            border: none;
            transition: all 0.3s ease;
            margin-top: 15px;
        }

        .btn-refresh:hover {
            background-color: #15315a;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(26, 58, 108, 0.3);
        }


        .transactions-table th {
            background-color: #f0f4f9;
            padding: 16px;
            text-align: left;
            font-weight: 600;
            color: #1a3a6c;
            border-bottom: 2px solid #ddd;
            cursor: pointer;
        }

        .transactions-table th:hover {
            background-color: #e0e8f0;
        }

        .transactions-table td {
            padding: 16px;
            border-bottom: 1px solid #eee;
        }

        .rules-list li {
            margin-bottom: 5px;
            font-size: 14px;
            display: flex;
            align-items: flex-start;
            gap: 8px;
        }

        .rules-list li i {
            color: #27ae60;
            font-size: 12px;
            margin-top: 5px;
        }

        .digit-input:focus {
            border-color: #1a3a6c;
            box-shadow: 0 0 5px rgba(26, 58, 108, 0.3);
        }


        .digit-container label {
            position: absolute;
            top: -20px;
            left: 0;
            font-size: 12px;
            color: #666;
        }


        /* 响应式设计 */
        @media (max-width: 900px) {
            .app-container {
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
            }
        }

        @media (max-width: 768px) {
            .balance-amount {
                font-size: 36px;
            }

            .account-details {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 576px) {
            .topbar {
                padding: 0 20px;
                height: 70px;
                flex-direction: column;
                justify-content: center;
                gap: 5px;
            }

            .page-title {
                font-size: 20px;
            }

            .content-area {
                padding: 20px;
            }

            .balance-info-card {
                padding: 20px;
            }

            .balance-amount {
                font-size: 28px;
            }
        }

        .error-message {
            color: #e74c3c;
            font-weight: 500;
            margin-top: 10px;
            text-align: center;
            display: none;
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- 左侧菜单 -->
        <div class="sidebar">
            <div class="logo-container">
                <div class="logo">
                    <div class="logo-icon">
                        <i class="fas fa-university"></i>
                    </div>
                    <div class="logo-text">银行系统</div>
                </div>
            </div>

            <div class="user-info">
                <div class="avatar" id="user-avatar"></div>
                <div class="user-details">
                    <div class="user-name" id="user-name"></div>
                    <div class="user-account" id="user-account"></div>
                </div>
            </div>

            <div class="sidebar-menu">
                <div class="menu-item active">
                    <i class="fas fa-wallet"></i>
                    <div class="menu-text">账户信息</div>
                </div>
                <div class="menu-item" onclick="location.href='user/Deposit.html';">
                    <i class="fas fa-money-bill-wave"></i>
                    <div class="menu-text">存款</div>
                </div>
                <div class="menu-item" onclick="location.href='user/Withdraw money.html';">
                    <i class="fas fa-hand-holding-usd"></i>
                    <div class="menu-text">取款</div>
                </div>
                 <div class="menu-item" onclick="location.href='user/Transfer.html';">
                    <i class="fas fa-exchange-alt"></i>
                    <div class="menu-text">转账</div>
                </div>
                <div class="menu-item" onclick="location.href='user/AccountLost.html';">
                    <i class="fas fa-lock"></i>
                    <div class="menu-text">账户挂失</div>
                </div>
                <div class="menu-item" onclick="location.href='user/ChangePassword.html';">
                    <i class="fas fa-key"></i>
                    <div class="menu-text">修改密码</div>
                </div>
                <div class="menu-item" onclick="location.href='user/TransactionRecords.html';">
                    <i class="fas fa-history"></i>
                    <div class="menu-text">交易记录</div>
                </div>
                <div class="menu-item" id="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <div class="menu-text">安全退出</div>
                </div>
            </div>
        </div>

        <!-- 右侧内容区 -->
        <div class="main-content">
            <div class="topbar">
                <div class="page-title">
                    <i class="fas fa-wallet"></i>
                    <span>余额查询</span>
                </div>
                <div class="current-time" id="current-time"></div>
            </div>

            <div class="content-area">
                <div class="balance-container">
                    <!-- 账户信息卡片 -->
                    <div class="balance-info-card">
                        <h2 class="section-title">
                            <i class="fas fa-user-circle"></i>账户信息
                        </h2>

                        <div class="account-details">
                            <div class="detail-item">
                                <div class="detail-label">账户名称</div>
                                <div class="detail-value" id="account-name"></div>
                            </div>

                            <div class="detail-item">
                                <div class="detail-label">银行账号</div>
                                <div class="detail-value" id="account-number"></div>
                            </div>
                            <div class="detail-item">
                                <div class="detail-label">账户状态</div>
                                <div class="detail-value" id="account-status">正常</div>
                            </div>
                        </div>

                        <div class="balance-display">
                            <div class="balance-label">当前账户余额</div>
                            <div class="balance-amount">
                                <span class="balance-currency">¥</span>
                                <span id="balance-amount">--</span>
                            </div>

                            <button class="btn-refresh" id="refresh-balance">
                                <i class="fas fa-sync-alt"></i> 刷新余额
                            </button>
                            <div class="error-message" id="error-message"></div>
                        </div>

                        <div class="detail-item">
                            <div class="detail-label">最后更新时间</div>
                            <div class="detail-value" id="last-update">--</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
            // 用户信息处理函数
            function renderUserInfo() {
            const cardID = sessionStorage.getItem('cardID');
            const userName = sessionStorage.getItem('userName');
            const balance = sessionStorage.getItem('balance'); // 获取存储的余额

            if (!cardID) {
                alert('请先登录系统');
                window.location.href = 'login.html';
                return;
            }

            const formattedCardID = cardID.replace(/(\d{4})(?=\d)/g, '$1 ');
            document.getElementById('user-account').textContent = `账号: ${formattedCardID}`;
            document.getElementById('user-name').textContent = userName;
            document.getElementById('user-avatar').textContent = userName.charAt(0);

            // 设置账户信息
            document.getElementById('account-name').textContent = userName;
            document.getElementById('account-number').textContent = formattedCardID;

            // 直接显示存储的余额
            if (balance) {
                document.getElementById('balance-amount').textContent =
                    parseFloat(balance).toLocaleString('zh-CN', { minimumFractionDigits: 2 });
                setLastUpdateTime(); // 设置更新时间
            }
        }

        // 更新时间显示
        function updateTime() {
            const now = new Date();
            const options = {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit',
                hour12: false
            };
            document.getElementById('current-time').textContent = now.toLocaleDateString('zh-CN', options);
        }

        // 设置最后更新时间
        function setLastUpdateTime() {
            const now = new Date();
            document.getElementById('last-update').textContent = now.toLocaleString('zh-CN');
        }

                // 获取余额
        async function getBalance() {
                const cardID = sessionStorage.getItem('cardID');
                if (!cardID) {
                    alert('请先登录系统');
                    window.location.href = 'login.html';
                    return;
                }

                // 显示加载状态
                const button = document.getElementById('refresh-balance');
                const errorMessage = document.getElementById('error-message');
                const originalHtml = button.innerHTML;
                button.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 查询中...';
                button.disabled = true;
                errorMessage.style.display = 'none';

                try {
                    // 发送请求到后端API
                    const response = await fetch(`http://localhost:8080/customer/balance?cardID=${cardID}`, {
                        method: 'GET',
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    });

                    if (!response.ok) {
                        throw new Error(`服务器错误: ${response.status}`);
                    }

                    const result = await response.json();

                    if (result.code === '200') {
                        const latestBalance = result.data;

                        sessionStorage.setItem('balance', latestBalance);
                        document.getElementById('balance-amount').textContent =
                            parseFloat(latestBalance).toLocaleString('zh-CN', { minimumFractionDigits: 2 });

                        // 更新最后更新时间
                        setLastUpdateTime();

                        // 恢复按钮状态
                        button.innerHTML = originalHtml;
                        button.disabled = false;

                        // 添加视觉反馈
                        button.style.backgroundColor = '#27ae60';
                        setTimeout(() => {
                            button.style.backgroundColor = '';
                        }, 1000);
                    } else {
                        throw new Error(result.message || '获取余额失败');
                    }
                } catch (error) {
                    console.error('获取余额出错:', error);
                    button.innerHTML = '<i class="fas fa-sync-alt"></i> 刷新余额';
                    button.disabled = false;

                    // 显示错误信息
                    errorMessage.textContent = `错误: ${error.message}`;
                    errorMessage.style.display = 'block';

                    // 设置红色警告样式
                    button.style.backgroundColor = '#e74c3c';
                    setTimeout(() => {
                        button.style.backgroundColor = '';
                    }, 1500);
                }
            }

        // 退出登录
        document.getElementById('logout-btn').addEventListener('click', function() {
            if(confirm('确定要退出系统吗？')) {
                sessionStorage.removeItem('cardID');
                sessionStorage.removeItem('userName');
                window.location.href = 'login.html';
            }
        });

        document.addEventListener('DOMContentLoaded', function () {
            renderUserInfo(); // 直接显示存储的余额
            updateTime();

            // 每秒更新一次时间
            setInterval(updateTime, 1000);

            // 刷新余额按钮事件
            document.getElementById('refresh-balance').addEventListener('click', getBalance);
        });
    </script>
</body>
</html>